/*******************************************************************************
* Project: QSAR(Cute Software Architecture) 
*
* Copyright: Copyright(C) 2024 by YuetingBen, All Rights Reserved
*
* File name: Tm.c
*
* Brief: ;
*
* Author: Yueting.Ben
*
* Module: Tm
*
*******************************************************************************/

/** HEADER FILES **/
#include "Tm.h"


/** MACRO DEFINITION **/
#define TM_TIMER_INVALID_VALUE    0xFFFF


/** TYPEDEF DEFINITION **/
typedef struct
{
    uint16 TargetTimer;
    uint16 TimerCounter;
    uint8 ExpiredFlag;
}Tm_TimerType;


/** LOCAL DATA DECLARATION **/
static Tm_TimerType Tm_Timer[TmConf_TimerChannel_Max];


/** GLOBAL DATA DECLARATION **/


/** LOCAL FUNCTION DECLARATION **/


/** GLOBAL FUNCTION DECLARATION **/

Std_ReturnType Tm_StartTimer(
    Tm_TimerIdType TimerId,
    uint16 TargetTimer
);

Std_ReturnType Tm_StopTimer(
    Tm_TimerIdType TimerId
);

Std_ReturnType Tm_GetTimerExpiredStatus(
    Tm_TimerIdType TimerId,
    uint8* ExpiredStatus
);

Std_ReturnType Tm_ClearTimerExpiredStatus(
    Tm_TimerIdType TimerId
);

void Tm_GetVersionInfo(
    Std_VersionInfoType* versionInfo
);

void Tm_MainFunction_Cyclic_1ms(
    void
);


/** LOCAL DATA **/
static Tm_TimerType Tm_Timer[TmConf_TimerChannel_Max] = {0x00};


/** GLOBAL DATA **/


/** LOCAL FUNCTION **/


/** GLOBAL FUNCTION **/

/*******************************************************************************
* -Name            Tm_StartTimer
* -Brief           Start timer, which will expired after running TargetTimer, uint ms.  
* -Details         None
* -Param[in]       TimerId: Identification of the NM-Timer
* -Param[in]       TargetTimer: Target timer
* -Param[out]      None
* -Return          E_OK: No error
*                  E_NOT_OK: Requesting failed          
*******************************************************************************/
Std_ReturnType Tm_StartTimer(
    Tm_TimerIdType TimerId,
    uint16 TargetTimer
)
{
    Std_ReturnType retVal = E_OK;
    
    Tm_TimerType* Tm_TimerPtr = (Tm_TimerType*)&Tm_Timer[TimerId];
    Tm_TimerPtr->TimerCounter = 0u;
    Tm_TimerPtr->TargetTimer = TargetTimer;
    Tm_TimerPtr->ExpiredFlag = RESET;

    return(retVal);
}

/*******************************************************************************
* -Name            Tm_StopTimer
* -Brief           Stop timer.  
* -Details         None
* -Param[in]       TimerId: Identification of the NM-Timer
* -Param[out]      None
* -Return          E_OK: No error
*                  E_NOT_OK: Requesting failed          
*******************************************************************************/
Std_ReturnType Tm_StopTimer(
    Tm_TimerIdType TimerId
)
{
    Std_ReturnType retVal = E_OK;
    
    Tm_TimerType* Tm_TimerPtr = (Tm_TimerType*)&Tm_Timer[TimerId];
    Tm_TimerPtr->TimerCounter = 0u;
    Tm_TimerPtr->TargetTimer = 0u;
    Tm_TimerPtr->ExpiredFlag = SET;

    return(retVal);
}

/*******************************************************************************
* -Name            Tm_GetTimerExpiredStatus
* -Brief           Get timer expired status.  
* -Details         None
* -Param[in]       TimerId: Identification of the NM-Timer
* -Param[out]      ExpiredStatus: SET: timer expired, RESET: timer not expired
* -Return          E_OK: No error
*                  E_NOT_OK: Requesting failed          
*******************************************************************************/
Std_ReturnType Tm_GetTimerExpiredStatus(
    Tm_TimerIdType TimerId,
    uint8* ExpiredStatus
)
{
    Std_ReturnType retVal = E_OK;
    
    Tm_TimerType* Tm_TimerPtr = (Tm_TimerType*)&Tm_Timer[TimerId];
    *ExpiredStatus = Tm_TimerPtr->ExpiredFlag;

    return(retVal);
}

/*******************************************************************************
* -Name            Tm_ClearTimerExpiredStatus
* -Brief           Clear timer expired status.  
* -Details         None
* -Param[in]       TimerId: Identification of the NM-Timer
* -Param[out]      None
* -Return          E_OK: No error
*                  E_NOT_OK: Requesting failed          
*******************************************************************************/
Std_ReturnType Tm_ClearTimerExpiredStatus(
    Tm_TimerIdType TimerId
)
{
    Std_ReturnType retVal = E_OK;
    
    Tm_TimerType* Tm_TimerPtr = (Tm_TimerType*)&Tm_Timer[TimerId];
    Tm_TimerPtr->TimerCounter = 0u;
    Tm_TimerPtr->TargetTimer = TM_TIMER_INVALID_VALUE;
    Tm_TimerPtr->ExpiredFlag = RESET;

    return(retVal);
}


/*******************************************************************************
* -Name         Tm_GetVersionInfo
* -Brief        Returns the version information of this module 
* -Details      None
* -Param[out]   versioninfo: Pointer to the version information of this module
* -Return       None          
*******************************************************************************/
void Tm_GetVersionInfo(
    Std_VersionInfoType* versionInfo
)
{
    versionInfo->vendorID = (uint16)YSAR_VENDOR_ID;
    versionInfo->moduleID = (uint8)TM_MODULE_ID;
    versionInfo->sw_major_version = (uint8)TM_SW_MAJOR_VERSION;
    versionInfo->sw_minor_version = (uint8)TM_SW_MINOR_VERSION;
    versionInfo->sw_patch_version = (uint8)TM_SW_PATCH_VERSION;
}


/*******************************************************************************
* -Name            Tm_MainFunction_Cyclic_1ms
* -Brief           Cyclic function of the CanNm which processes the timer 
* -Details         None
* -Param[in]       None
* -Param[out]      None
* -Return          None          
*******************************************************************************/
void Tm_MainFunction_Cyclic_1ms(
    void
)
{   
    Tm_TimerIdType timerId;

    for(timerId = 0u; timerId < TmConf_TimerChannel_Max; timerId++)
    {
        Tm_TimerType* Tm_TimerPtr = (Tm_TimerType*)&Tm_Timer[timerId];

        if(Tm_TimerPtr->TargetTimer >  (Tm_TimerPtr->TimerCounter + 1u))
        {
            Tm_TimerPtr->TimerCounter++;
        }
        else
        {
            if(TM_TIMER_INVALID_VALUE != Tm_TimerPtr->TargetTimer)
            {
                if(RESET == Tm_TimerPtr->ExpiredFlag) 
                {
                    Tm_TimerPtr->ExpiredFlag = SET;
                }
            }
        }
    }
}


